﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Model;
using Register.Command;
using Register.DAL;
using System.Web.Script.Serialization;
using System.Data;
using System.Text;
namespace RegisterWeb.Manager.HospitalManager.ashx
{
    /// <summary>
    /// RoleService 的摘要说明
    /// </summary>
    public class HospitalHandler: IHttpHandler
    {
       public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            if (action.Equals("add"))
             {
                HospitalInfo hospital = new HospitalInfo();

                HttpPostedFile hpf = HttpContext.Current.Request.Files["Hospital_Logo"];
                FileOperate upFiles = new FileOperate();
                upFiles.FileType = "gif|jpg|bmp";
                upFiles.Sizes = 1500;
                upFiles.Path = System.Web.HttpContext.Current.Server.MapPath("~/Upload/Pictures");
                upFiles.Status = 0;
                string picName = upFiles.fileSaveAs(hpf, false); 

                hospital.Hospital_ID = DataIDHelper.GetDataID("Hospital_ID");
                hospital.City_ID = context.Request["City_ID"]; 
                hospital.Hospital_Recoder = "DoubleZ";//临时赋值
                hospital.Hospital_Name = context.Request["Hospital_Name"];
                hospital.Hospital_Level = context.Request["Hospital_Level"];
                hospital.Hospital_Nature = context.Request["Hospital_Nature"];
                hospital.Hospital_Type = context.Request["Hospital_Type"];
                hospital.Hospital_Address = context.Request["Hospital_Address"];
                hospital.Hospital_Phone = context.Request["Hospital_Phone"];
                hospital.Hospital_Website = context.Request["Hospital_Website"];
                hospital.Hospital_Introduce = context.Request["Hospital_Introduce"];
                //少了医院logo,对logo进行图片验证
                hospital.Hospital_Logo = picName;
                hospital.Hospital_AddTime = DateTime.Now;
                hospital.Hospital_Recoder = context.Request["Hospital_Recoder"];
                hospital.Hospital_No = hospital.Hospital_ID;   //临时将ID值赋给编号
                hospital.Hospital_LastAmendTime = hospital.Hospital_AddTime;
                hospital.Hospital_LastAmendPerson = "无";
                hospital.Hospital_State = "1";
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(HospitalInfoBLL.AddHospitalInfo(hospital), "医院添加成功！")));

             }
                //添加省区
            else if (action.Equals("addPro")) {
                 Province province = new Province();

                province.Province_ID= DataIDHelper.GetDataID("Province_ID");
              
                 province.Province_Name = context.Request["Province_Name"];
              
                 province.Province_AddTime = DateTime.Now;
             
                 province.Province_Recoder = "DoubleZ";
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(ProvinceOrCityBLL.AddProvinceInfo(province), "省区添加成功！")));
            }
            else if (action.Equals("addCity"))
            {
                City city = new City();

                city.City_ID = DataIDHelper.GetDataID("City_ID");
                city.Province_ID = context.Request["Province_ID"];

                city.CIty_Name = context.Request["CIty_Name"];

                city.City_AddTime = DateTime.Now;

                city.City_Recoder= "DoubleZ";
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(ProvinceOrCityBLL.AddCityInfo(city), "城市添加成功！")));
            }

            #region 检查医院是否已经配置
            else if (action.Equals("checkInstall"))
            {
                string hospitalId = context.Request["id"];
                if (HospitalInfoBLL.Exists(hospitalId))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }


            #endregion
                //获取医院表格数据
            else if (action.Equals("get"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<HospitalInfo> list = new List<HospitalInfo>();
                list = HospitalInfoBLL.GetPagedHospitalInfo(row, page, " 1=1 ");

                List<HospitalInfoJSON> json = new List<HospitalInfoJSON>();
                foreach (HospitalInfo hospital in list)
                { //对于每一个医院对象，根据获得的城市ID获得城市名，并绑定


                    json.Add(new HospitalInfoJSON(ProvinceOrCityBLL.GetProvinceNameById(hospital.City_ID), ProvinceOrCityBLL.GetCityNameById(hospital.City_ID), hospital.Hospital_Address, hospital.Hospital_AddTime.ToString(), hospital.Hospital_ID, hospital.Hospital_Introduce, hospital.Hospital_LastAmendPerson, hospital.Hospital_LastAmendTime.ToString(), HospitalInfoBLL.GetParamNameById(hospital.Hospital_Level), hospital.Hospital_Level,hospital.Hospital_Logo, hospital.Hospital_Name, HospitalInfoBLL.GetParamNameById(hospital.Hospital_Nature), hospital.Hospital_Nature,hospital.Hospital_No, hospital.Hospital_Phone, hospital.Hospital_Recoder, hospital.Hospital_State, HospitalInfoBLL.GetParamNameById(hospital.Hospital_Type),hospital.Hospital_Type, hospital.Hospital_Website));
                }

                HospitalInfoListJSON HospitalListJSON = new HospitalInfoListJSON();
                HospitalListJSON.total = HospitalInfoBLL.GetHospitalInfoCount(null);
                HospitalListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(HospitalListJSON));

            }
            else if (action.Equals("delete"))
            {
                String ID = context.Request["id"];
                if (HospitalInfoBLL.DeleteHospitalInfo(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            else if (action.Equals("edit"))
            {
                HospitalInfo hospital = new HospitalInfo();
                hospital.Hospital_ID = context.Request["id"];
                hospital.Hospital_Name = context.Request["Hospital_Name"];
                hospital.Hospital_Level = context.Request["Hospital_Level"];
                hospital.Hospital_Nature = context.Request["Hospital_Nature"];
                hospital.Hospital_Type = context.Request["Hospital_Type"];
                hospital.Hospital_Address = context.Request["Hospital_Address"];
                hospital.Hospital_Phone = context.Request["Hospital_Phone"];
                hospital.Hospital_Website = context.Request["Hospital_Website"];
                hospital.Hospital_Introduce = context.Request["Hospital_Introduce"];
                //少了医院logo,对logo进行图片验证
               // hospital.Hospital_Logo = context.Request["Hospital_Logo"];
                if (String.IsNullOrEmpty(context.Request["Hospital_Logo"]) || context.Request["Hospital_Logo"] == null)
                {
                    hospital.Hospital_Logo = context.Request["hidden"]; 
                  
                }
                else {
                    
                    hospital.Hospital_Logo =context.Request["Hospital_Logo"]; }
                hospital.Hospital_LastAmendPerson = "DoubleZ";//临时赋值
                hospital.Hospital_LastAmendTime = DateTime.Now;


                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(HospitalInfoBLL.UpdateHospitalInfo(hospital), "编辑用户成功！")));

            }
            #region  绑定数据表格
           /* if (action.Equals("get"))
            {
                DataSet ds = HospitalInfoBLL.GetList("Hospital_State=1");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                DataTable dt = ds.Tables[0];
                List<HospitalInfo> list = HospitalInfoBLL.DataTableToList(dt);
                context.Response.Write(jss.Serialize(list));
            }
           */
            #endregion
            #region  删除记录
           /* if (action.Equals("delete")) {
                string HospitalID = context.Request["id"];
                string data = "";
                if (HospitalInfoBLL.Delete(HospitalID))
                {
                    data = "{success:true,message:\"删除成功\"}";
                    context.Response.Write(data);
                }
                else
                {
                    data = "{success:true,message:\"删除失败\"}";
                    context.Response.Write(data);
                }
            }*/
            #endregion
            #region  绑定医院下拉列表

            List<Parameter> list1;
            List<Province> list2;
            List<City> list3;
            JavaScriptSerializer jss1 = new JavaScriptSerializer();
            if (action.Equals("shen")) {

                list2 = ProvinceOrCityBLL.GetProvinceList();

                context.Response.Write(jss1.Serialize(list2));
            }
            if (action.Equals("shi"))
            {

                list3 = ProvinceOrCityBLL.GetCityList();

                context.Response.Write(jss1.Serialize(list3));
            }
                //绑定等级
  
            if (action.Equals("level"))
            {

                 list1 = ParamterHelper.GetParametersByType("Hospital_Level");
             
                  context.Response.Write(jss1.Serialize(list1));
            }
             //绑定性质
            if (action.Equals("nature"))
            {
                list1 = ParamterHelper.GetParametersByType("Hospital_Nature");
                context.Response.Write(jss1.Serialize(list1));
            }
           
            //绑定类别
            if (action.Equals("type")) {
                list1 = ParamterHelper.GetParametersByType("Hospital_Type");
                context.Response.Write(jss1.Serialize(list1));
            }

            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class message
{
    public message(bool success, string msg)
    {
        this.msg = msg;
        this.success = success;
    }
    public bool success { get; set; }
    public string msg { get; set; }
}
 class HospitalInfoListJSON
    {

        public int total { get; set; }
        public List<HospitalInfoJSON> rows { get; set; }

    }

    class HospitalInfoJSON
    {
        	public string Hospital_ID
		{
			set;get;
		}
            public string Province_ID
            {
                get;
                set;
            }
		public string City_ID
		{
			get;set;
		}
	    
		public string Hospital_No
		{
			get;set;
		}
		
		public string Hospital_Name
		{
			get;set;
		}
		
		public string Hospital_Level
		{
			get;set;
		}
        public string Hospital_LevelValue
        {
            get;
            set;
        }
		
		public string Hospital_Nature
		{
			get;set;
		}
        public string Hospital_NatureValue
        {
            get;
            set;
        }
		
		public string Hospital_Type
		{
			get;set;
		}
        public string Hospital_TypeValue
        {
            get;
            set;
        }
		public string Hospital_Address
		{
			get;set;
		}
		
		public string Hospital_Phone
		{
			get;set;
		}
		
		public string Hospital_Website
		{
			get;set;
		}
	
		public string Hospital_Introduce
		{
			get;set;
		}
	
		public string Hospital_Logo
		{
		get;set;
		}
		
		public string Hospital_AddTime
		{
			get;set;
		}
		
		public string Hospital_Recoder
		{
			get;set;
		}
	
		public string Hospital_LastAmendTime
		{
			get;set;
		}
		
		public string Hospital_LastAmendPerson
		{
			get;set;
		}
		
		public string Hospital_State
		{
			get;set;
		}
        public HospitalInfoJSON(String Province_ID, String City_ID, String Hospital_Address, String Hospital_AddTime, String Hospital_ID, String Hospital_Introduce, String Hospital_LastAmendPerson, String Hospital_LastAmendTime, String Hospital_Level, String Hospital_LevelValue, String Hospital_Logo, String Hospital_Name, String Hospital_Nature, String Hospital_NatureValue,String Hospital_No, String Hospital_Phone, String Hospital_Recoder, String Hospital_State, String Hospital_Type,String  Hospital_TypeValue, String Hospital_Website)
        {
            this.Province_ID = Province_ID;
           this.Hospital_ID=Hospital_ID;
            this.City_ID=City_ID;
            this.Hospital_No=Hospital_No;
            this.Hospital_Name=Hospital_Name;
            this.Hospital_Level=Hospital_Level;
            this.Hospital_LevelValue = Hospital_LevelValue;
            this.Hospital_Nature=Hospital_Nature;
            this.Hospital_NatureValue = Hospital_NatureValue;
            this.Hospital_Type=Hospital_Type;
            this.Hospital_TypeValue = Hospital_TypeValue;
            this.Hospital_Address=Hospital_Address;
            this.Hospital_Phone=Hospital_Phone;
            this.Hospital_Website=Hospital_Website;
            this.Hospital_Introduce=Hospital_Introduce;
            this.Hospital_Logo=Hospital_Logo;
            this.Hospital_Recoder=Hospital_Recoder;
            this.Hospital_AddTime=Hospital_AddTime;
            this.Hospital_LastAmendTime=Hospital_LastAmendTime;
            this.Hospital_LastAmendPerson=Hospital_LastAmendPerson;
            this.Hospital_State=Hospital_State;
        }

    }
      